<template>

  <div>

    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true" v-if="hasAuth('position:add')">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="deptData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        stripe
        default-expand-all
        :tree-props="{children: 'menus', hasChildren: 'hasMenus'}">

      <el-table-column
          prop="name"
          label="名称"
          sortable
          width="180">
      </el-table-column>

      <el-table-column
          prop="is_show"
          label="状态">
        <template v-slot:default="scope">
          <el-tag size="small" v-if="scope.row.is_show" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.is_show" type="danger">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          prop="create_time"
          width="300"
          label="创建时间"
      >
      </el-table-column>
      <el-table-column
          prop="school.name"
          width="200"
          label="所属学校"
      >
      </el-table-column>
      <el-table-column
          prop="description"
          width="200"
          label="描述"
      >
      </el-table-column>
      <el-table-column
          prop="icon"
          label="操作">
        <template v-slot:default="scope">
          <el-button type="text" @click="editHandle(scope.row.id)" v-if="hasAuth('position:update')">编辑</el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)"
                           v-if="hasAuth('position:delete')">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="部门名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="是否显示" prop="is_show" label-width="100px">
          <el-radio-group v-model="editForm.is_show">
            <el-radio :label='false'>禁用</el-radio>
            <el-radio :label='true'>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="顶级部门" prop="is_top" label-width="100px">
          <el-radio-group v-model="editForm.is_top"
                          @input="chooseParentDept=!editForm.is_top">
            <el-radio :label=true>是</el-radio>
            <el-radio :label=false>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级部门" prop="parent" v-show="chooseParentDept">
          <treeselect
              style="width: 170px"
              placeholder="选择部门"
              v-model="editForm.parent"
              :options="deptData"
              :normalizer="normalizer"
          />
        </el-form-item>
        <el-form-item style="margin-bottom: 0;" label="学校:" prop="school_id">
          <el-select
              v-model="editForm.school_id"
              style="width: 437px"
              placeholder="请选择">
            <el-option
                v-for="item in schoolData"
                :key="item.name"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍:">
          <el-input type="textarea" v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {api} from "@/assets/static/js/settings";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: "PositionView",
  components: {Treeselect},
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      dialogVisible: false,
      editForm: {},

      tableData: [],
      deptData: [],
      schoolData: [],
      editFormRules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
        school_id: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
      multipleSelection: [],

      permDialogVisible: false,
      permForm: {},
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permTreeData: [],
      // 新增部门，上级部门是否显示
      chooseParentDept: false

    }
  },
  created() {
    this.getDeptList()
  },
  methods: {
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children: node.menus
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    // 获取数据
    getDeptList() {
      this.$axios.get(api.position).then(res => {
        if (res) {
          this.tableData = res.results
        }
      })
      this.$axios.get(api.position_list).then(res => {
        if (res) {
          this.deptData = res.results
        }
      })
      this.$axios.get(api.school).then(res => {
        if (res) {
          this.schoolData = res.results
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.editForm)
          let post_type = this.editForm.id ? this.$axios.put : this.$axios.post
          post_type(api.position + (this.editForm.id ? this.editForm.id + '/' : ''), this.editForm)
              .then(res => {
                    if (res) {
                      this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        onClose: () => {
                          this.getDeptList()
                        }
                      });
                      this.dialogVisible = false
                      this.resetForm(formName)
                    }
                  }
              )
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 删除功能
    delHandle(id) {
      this.$axios.delete(api.position + id + "/").then(res => {
        if (res) {
          this.$message({
            showClose: true,
            message: '恭喜你，操作成功',
            type: 'success',
            onClose: () => {
              this.getDeptList()
            }
          });
        }
      })
    },
    // 获取待修改的部门数据
    editHandle(id) {
      this.$axios.get(api.position + id + '/').then(res => {
        if (res) {
          this.editForm = res.results
          this.dialogVisible = true
        }
      })
    },
  }
}
</script>

<style scoped>

</style>